package cn.edu.uestc.controller;

import cn.edu.uestc.pojo.Order;
import cn.edu.uestc.pojo.Product;
import cn.edu.uestc.service.StockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class StockController {
    @Autowired
    StockService stockService;
    @Autowired
    HttpServletRequest request;

    @PostMapping("update_stock")
    public boolean updateStock(String productName, int productNum) {
        String shopName = (String) request.getSession().getAttribute("username");
        stockService.updateStock(new Product(productName, shopName, productNum));
        return true;
    }

    @PostMapping("get_products_by_shop_name")
    public List<String> getProductsByShopName(String shopName) {
        return stockService.getProductsByShopName(shopName);
    }

    @GetMapping("get_all_products")
    public Map<String, Object> getAllProducts() {
        Map<String, Object> map = new HashMap<>();
        String shopName = (String) request.getSession().getAttribute("username");
        List<Product> productList = stockService.getAllProducts(shopName);
        map.put("code", "0");
        map.put("count", productList.size());
        map.put("data", productList);
        return map;
    }

    @PostMapping("delete_product_by_name")
    public boolean deleteProductByName(String productName, String shopName) {
        return stockService.deleteProductByName(productName, shopName);
    }
}
